// pages/addressmgr/addressmgr.js
const Protocol = require("../../Controls/Protocol")
const DataExchange = require("../../Controls/DataExchange")
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressInfo:
    [
      //{id:1, name: "曹建伟", phonenum:"18633402321", city: "北京 东城区", detail:"北京东城区爱是劳动法即可", checked:false},
        //{ id: 2, name: "曹建伟2", phonenum: "18633402322", city: "上海 普陀区", detail: "上海普陀区爱是劳动法即可", checked: true },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.isSelect = options.isSelect;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ addressInfo: app.globalData.address });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  btnClickItemIvk: function (event)
  {
    if(!this.isSelect)
    {
      return;
    }

    var id = parseInt(event.currentTarget.dataset.item.id);
    DataExchange.set_selectedAddress(id)
    wx.navigateBack({ 
    })
  },

  btnAddAddressIvk:function()
  {
    wx.navigateTo({
      url: '../addressedit/addressedit',
    })
  },

  btnEditIvk: function (event)
  {
    var id = event.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: `../addressedit/addressedit?addressID=${id}`,
    })
  },

  btnDelIvk: function (event)
  {
    const that = this;
    var id = event.currentTarget.dataset.item.id;
    Protocol.delAddress(id, {
      success: () => { 
        that.setData({ addressInfo: app.globalData.address})
      }
    });
  },

  onCheckChanged:function(event)
  {
    for(var i = 0; i < this.data.addressInfo.length; i++)
    {
      if(this.data.addressInfo[i].id.toString() == event.detail.value)
      {
        Protocol.checkAddress(this.data.addressInfo[i].id);
        break;
      }
    }
  }
})